﻿Public Class Form1


    ' Create a Random object to generate random numbers.
    Private randomizer As New Random

    ' This Integer will keep track of the time left.
    Private timeLeft As Integer

    ' These Integers will store the numbers
    ' for the addition and subtraction problems.
    Private addend1 As Integer
    Private addend2 As Integer
    Private subend1 As Integer
    Private subend2 As Integer
    Private proend1 As Integer
    Private proend2 As Integer
    Private divend1 As Integer
    Private divend2 As Integer

    Private Sub StartTheQuiz()

        ' Fill in the addition problem.
        addend1 = randomizer.Next(51)
        addend2 = randomizer.Next(51)

        plusLeftLabel.Text = addend1.ToString()
        plusRightLabel.Text = addend2.ToString()
        sum.Value = 0

        ' Fill in the subtraction problem.
        subend1 = randomizer.Next(51)
        subend2 = randomizer.Next(51)

        minusLeftLabel.Text = subend1.ToString()
        minusRightLabel.Text = subend2.ToString()
        difference.Value = 0

        ' Fill in the multiplication problem.
        proend1 = randomizer.Next(51)
        proend2 = randomizer.Next(51)

        timesLeftLabel.Text = proend1.ToString()
        timesRightLabel.Text = proend2.ToString()
        product.Value = 0

        ' Fill in the division problem.
        divend1 = randomizer.Next(51)
        divend2 = randomizer.Next(51)

        dividedLeftLabel.Text = divend1.ToString()
        dividedRightLabel.Text = divend2.ToString()
        quotient.Value = 0

        ' Start the timer.
        timeLeft = 30
        timeLabel.Text = "30 seconds"
        Timer1.Start()

    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click

        Stop
        StartTheQuiz()
        startButton.Enabled = False

    End Sub

    ''' <summary>
    ''' Check the answer to see if the user got everything right.
    ''' </summary>
    ''' <returns>True if the answer's correct, false otherwise.</returns>
    ''' <remarks></remarks>
    Public Function CheckTheAnswer() As Boolean

        If addend1 + addend2 = sum.Value Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Sub ShowTheAnswer()

        sum.Value = addend1 + addend2
        difference.Value = subend1 - subend2
        product.Value = proend1 * proend2
        quotient.Value = divend1 / divend2

    End Sub

    Private Sub Timer1_Tick() Handles Timer1.Tick

        If CheckTheAnswer() Then
            ' If the user got the answers right, stop the timer and show a MessageBox.
            Timer1.Stop()
            MessageBox.Show("You got all of the answers right!", "Congratulations!")
            startButton.Enabled = True
        ElseIf timeLeft > 0 Then
            ' Decrease the time left by one second and display
            ' the new time left by updating the Time Left label.
            timeLeft -= 1
            timeLabel.Text = timeLeft & " seconds"
        Else
            ' If the user ran out of time, stop the timer, show
            ' a MessageBox, and fill in the answers.
            Timer1.Stop()
            timeLabel.Text = "Time's up!"
            MessageBox.Show("You didn't finish in time.", "Sorry")
            Call ShowTheAnswer()
            startButton.Enabled = True
        End If

    End Sub

End Class
